package me.edzh.spring.bootstrap.model;


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import me.edzh.spring.bootstrap.utils.ResultCode;
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;

import static me.edzh.spring.bootstrap.utils.Constants.LOG_TRACE_ID;

/**
 * Created by Edward on 2017/2/7 007.
 */
@Builder
@Data
@AllArgsConstructor
public class Response <T> {

    private Long code;
    private String message;

    private T data;
    private String traceId;

    public static Response<Object> ok() {
        return new Response<>();
    }

    public static <K> Response<K>  ok(K data) {
        return new Response<>(data);
    }

    public Response(){
        this.code = ResultCode.OK_CODE;
        this.message = "ok";

        Object trace = MDC.get(LOG_TRACE_ID);
        if (trace != null) {
            this.traceId = trace.toString();
        }
    }

    public Response(Long code,String message){
        this();
        this.setCode(code);
        this.setMessage(message);
    }

    public Response(boolean isSuccess,Long code,String message){
        this();
        if(!isSuccess){
            this.code = code;
            this.message = message;
        }
    }

    public Response(T data){
        this();
        this.setData(data);
    }
}